package com.victor.service.impl;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.victor.dao.ActivityDao;
import com.victor.dao.GoodsDao;
import com.victor.entity.Cart;
import com.victor.entity.JingqubusinessShop;
import com.victor.entity.Jingqubusinessshop_photo;
import com.victor.service.ActivityService;
import com.victor.service.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService{

	@Autowired
	private GoodsDao dao;

	/**
	 * search商品列表
	 */
	@Override
	public List<JingqubusinessShop> search(String search) {
		List<JingqubusinessShop> list = dao.search(search);
		for (int i = 0; i < list.size(); i++) {
			List<Jingqubusinessshop_photo> photos = dao.getPhotos(list.get(i).getId()+"");
			list.get(i).setPhotos(photos);
		}
		return list;
	}
	
	/**
	 * 根据商品id获取商品销售量
	 */
	@Override
	public int getSum(String id) {
		int sum = 0;
		List<String> list = dao.getSum(id);
		for (int i = 0; i < list.size(); i++) {
			int a = Integer.parseInt(list.get(i));
			sum+=a;
		}
		return sum;
	}

	/**
	 * 先根据搜索框搜索商品再根据类型type查询商品列表
	 */
	@Override
	public List<JingqubusinessShop> selByType(String search,String type) {
		List<JingqubusinessShop> list = dao.selByType(search,type);
		for (int i = 0; i < list.size(); i++) {
	
			List<Jingqubusinessshop_photo> photos = dao.getPhotos(list.get(i).getId()+"");
			list.get(i).setPhotos(photos);
			//获取每件商品销量
			int sum = 0;
			List<String> num = dao.getSum(list.get(i).getId()+"");
			for (int j = 0; j < num.size(); j++) {
				int a = Integer.parseInt(num.get(j));
				sum+=a;
			}
			list.get(i).setSellcount(sum+"");
		}
		return list;
	}

	/**
	 * 根据id查看商品详情
	 */
	@Override
	public JingqubusinessShop goodsDetail(Integer id) {
		return dao.goodsDetail(id);
	}

	/**
	 * 添加购物车
	 */
	@Override
	public boolean addShopcart(Cart cart) {
		return dao.addShopcart(cart);
	}

	/**
	 * 	通过当前登录用户的id
	 * 	查看购物车列表
	 */
	@Override
	public List<Cart> selShopcart(String userid) {
		return dao.selShopcart(userid);
	}

	/**
	 * 修改购物车中的商品数量
	 */
	@Override
	public boolean updateCart(Cart cart) {
		return dao.updateCart(cart);
	}

	/**
	 * 批量删除购物车
	 */
	@Override
	public boolean batchDelete(int[] ids) {
		return dao.batchDelete(ids) > 0 ? true : false;
	}

	/**
	 * 根据商品id获取商品销量
	 */
	@Override
	public int getFavorites(String id) {
		return dao.getFavorites(id);
	}
}
